<?php

use yii\helpers\Html;
use yii\grid\GridView;
use backend\helpers\ActionButton;
use yii\helpers\StringHelper;
use backend\helpers\FileViewHelper;
use backend\models\Donor;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\DonorSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Доноры и партнёры';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="container donor-index">

    <h1 class="main-title"><?= Html::encode($this->title) ?></h1>

    <p>
        <?= ActionButton::create('Добавить информацию о доноре или партнёре', 'createDonor') ?>
    </p>

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'tableOptions' => [
            'class' => 'table table-striped'
        ],
        'columns' => [
            'name',
            [
                'attribute'=>'coop_contract_filename',
                'value' => function ($model) {
                    if($model->coop_contract_filename)
                    {
                        return Html::a(
                            'Скачать',
                            'https://storage.yandexcloud.net/' . Donor::BUCKET_NAME . '/' . $model->coop_contract_filename,
                        );
                    }
                    else
                    {
                        return '<span class="badge">нет</span>';
                    }
                    
                },
                'format' => 'raw'
            ],
            [
                'attribute' => 'coop_contract_date_end',
                'label' => 'Дата завершения договора',
                'filter' => Html::activeInput('date', $searchModel, 'coop_contract_date_end', ['class' => 'form-control']),
                'value' => function ($model) {
                    if($model->coop_contract_date_end && $model->coop_contract_date_end>date('Y-m-d'))
                    {
                        return '<span class="badge badge-success">' . \Yii::$app->formatter->asDate($model->coop_contract_date_end) . '</span>';
                    }
                    elseif($model->coop_contract_date_end && $model->coop_contract_date_end<date('Y-m-d'))
                    {
                        return '<span class="badge badge-danger">' . \Yii::$app->formatter->asDate($model->coop_contract_date_end) . '</span>';
                    }
                    else
                    {
                        return '<span class="badge badge-success">бессрочно</span>';
                    }
                },
                'format' => 'raw',
            ],
            [
                'attribute' => 'coop_contract_subject',
                'label' => 'Предмет договора',
                'contentOptions' => ['style' => 'width:250px; white-space: normal;'],
                'value' => function($model, $key, $index, $column) {
                    return StringHelper::truncate($model->coop_contract_subject, 70);
                },
                'format' => 'ntext',
            ],

            [
                'class' => 'yii\grid\ActionColumn',
                'visibleButtons' =>
                [
                    'view' => Yii::$app->user->can('browseDonor'),
                    'update' => Yii::$app->user->can('updateDonor'),
                    'delete' => Yii::$app->user->can('deleteDonor'),
                ]
            ],
        ],
    ]); ?>


</div>
